﻿using System;
using System.Collections.Generic;

using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace Demo
{
    public class VBO
    {
        public VBO(float[] vertices, uint[] indices)
        {
            size_vbo = vertices.Length;
            size_ibo = indices.Length;
            vbo = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(size_vbo * 4),
                vertices, BufferUsageHint.StaticDraw);

            //tbo = GL.GenBuffer();
            //GL.BindBuffer(BufferTarget.ArrayBuffer, tbo);
            //GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(texcoords.Length * 4),
            //    texcoords, BufferUsageHint.StaticDraw);

            ibo = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(size_ibo * 4),
                indices, BufferUsageHint.StaticDraw);
        }

        public int vbo { get; }
        public int ibo { get; }
        private int size_vbo;
        private int size_ibo;

        public void Draw()
        {
            GL.EnableVertexAttribArray(0);
            //GL.EnableVertexAttribArray(1);
            //GL.EnableClientState(ArrayCap.VertexArray);//2x

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            //GL.VertexPointer(3, VertexPointerType.Float, 3 * 4, new IntPtr(0));//2x
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * 4, 0);

            //GL.BindBuffer(BufferTarget.ElementArrayBuffer, tbo);
            //GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 2 * 4, 0);

            //GL.DrawArrays(PrimitiveType.Quads, 0, 4);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
            GL.DrawElements(PrimitiveType.Triangles, size_ibo, DrawElementsType.UnsignedInt, IntPtr.Zero);
        }

    }
}
